![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Note, if you're using @xmpp/client
or @xmpp/component
, you don't need to install @xmpp/xml
.
npm install @xmpp/xml
const xml = require("@xmpp/xml");
const { xml } = require("@xmpp/client");
const { xml } = require("@xmpp/component");
There's 2 methods for writing XML with xmpp.js
const xml = require("@xmpp/xml");
const recipient = "user@example.com";
const days = ["Monday", "Tuesday", "Wednesday"];
const message = xml(
"message",
{ to: recipient },
xml("body", {}, 1 + 2),
xml(
"days",
{},
days.map((day, idx) => xml("day", { idx }, day)),
),
);
If the second argument passed to xml
is a string
instead of an object
, it will be set as the xmlns
attribute.
// both are equivalent
xml("time", "urn:xmpp:time");
xml("time", { xmlns: "urn:xmpp:time" });
/** @jsx xml */
const xml = require("@xmpp/xml");
const recipient = "user@example.com";
const days = ["Monday", "Tuesday"];
const message = (
<message to={recipient}>
<body>{1 + 2}</body>
<days>
{days.map((day, idx) => (
<day idx={idx}>${day}</day>
))}
</days>
</message>
);
Requires a preprocessor such as TypeScript or Babel with @babel/plugin-transform-react-jsx.
The attrs
property is an object that holds xml attributes of the element.
message.attrs.to; // user@example.com
Returns the text value of an element
message.getChild("body").text(); // '3'
Get child element by name.
message.getChild("body").toString(); // '<body>3</body>'
Get child element text value.
message.getChildText("body"); // '3'
Get children elements by name.
message.getChild("days").getChildren("day"); // [...]
Since getChildren
returns an array, you can use JavaScript array methods such as filter and find to build more complex queries.
const days = message.getChild("days").getChildren("day");
// Find Monday element
days.find((day) => day.text() === "Monday");
days.find((day) => day.attrs.idx === 0);
// Find all days after Tuesday
days.filter((day) => day.attrs.idx > 2);
You can get the parent node using the parent property.
console.log(message.getChild("days").parent === message);
You can get the root node using the root method.
console.log(message.getChild("days").root() === message);
The attrs
property is an object that holds xml attributes of the element.
message.attrs.type = "chat";
Object.assign(message.attrs, { type: "chat" });
Set the text value of an element
message.getChild("body").text("Hello world");
Adds text or element nodes to the last position. Returns the parent.
message.append(xml("foo"));
message.append("bar");
message.append(days.map((day) => xml("day", {}, day)));
// <message>
// ...
// <foo/>
// bar
// <day>Monday</day>
// <day>Tuesday</day>
// </message>
Adds text or element nodes to the first position. Returns the parent.
message.prepend(xml("foo"));
message.prepend("bar");
message.prepend(days.map((day) => xml("day", {}, day)));
// <message>
// <day>Tuesday</day>
// <day>Monday</day>
// bar
// <foo/>
// ...
// </message>
Removes a child element.
const body = message.getChild("body");
message.remove(body);
You can embed JSON anywhere but it is recommended to use appropriate semantic.
/** @jsx xml */
// write
message.append(
<myevent xmlns="xmpp:example.org">
<json xmlns="urn:xmpp:json:0">{JSON.stringify(days)}</json>
</myevent>,
);
// read
JSON.parse(
message
.getChild("myevent", "xmpp:example.org")
.getChildText("json", "urn:xmpp:json:0"),
);
See also JSON Containers and Simple JSON Messaging.
@xmpp/xml
include a function to parse XML strings.
⚠ Use with care. Untrusted input or substitutions can result in invalid XML and side effects.
const { escapeXML, escapeXMLText };
const parse = require("@xmpp/xml/lib/parse");
const ctx = parse("<message><body>hello world</body></message>");
ctx.getChildText("body"); // hello world
If you must use with untrusted input, escape it with escapeXML
and escapeXMLText
.
const { escapeXML, escapeXMLText } = require("@xmpp/xml");
const parse = require("@xmpp/xml/lib/parse");
const message = parse(`
<message to="${escapeXML(to)}">
<body>${escapeXMLText(body)}</body>
</message>,
`);
FAQs
XMPP XML for JavaScript
The npm package @xmpp/xml receives a total of 15,001 weekly downloads. As such, @xmpp/xml popularity was classified as popular.
We found that @xmpp/xml demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.